﻿CloudDisk.SwfUploader = (function(){
    var uploader = null;

	function fileDialogComplete(numFilesSelected, numFilesQueued) {
		try {
		   if (numFilesSelected > 0) {
			   CloudDisk.UploadProgressDialog.show();
		   }

		   this.startUpload();
		} catch (ex) {
		   this.debug(ex);
		}
	}
	
	function fileQueuedHandler(file) {
		try {
			CloudDisk.UploadProgressDialog.addTask(file);
		} catch (ex) {
			this.debug(ex);
		}

	}

	/*
	file :{
    id : string,			// SWFUpload file id, used for starting or cancelling and upload
	index : number,			// The index of this file for use in getFile(i)
	name : string,			// The file name. The path is not included.
	size : number,			// The file size in bytes
	type : string,			// The file type as reported by the client operating system
	creationdate : Date,		// The date the file was created
	modificationdate : Date,	// The date the file was last modified
	filestatus : number,		// The file's current status. Use SWFUpload.FILE_STATUS to interpret the value.
	}
	*/
	function uploadStartHandler(file) {
		try {

		}
		catch (ex) {
		    this.debug(ex);
		}
		
		return true;
	}
	
	function uploadProgressHandler(file, bytesLoaded, bytesTotal) {
		try {
			var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
                
			file.progress = percent + '%';
			CloudDisk.UploadProgressDialog.update(file);
		} catch (ex) {
			this.debug(ex);
		}
	}

	function uploadSuccessHandler(file, serverData) {
		try {
			file.progress = '完成';
			CloudDisk.UploadProgressDialog.update(file);
		} catch (ex) {
			this.debug(ex);
		}
	}
	
	function fileQueueErrorHandler(file, errorCode, message) {
		try {
			if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			    var msg = "您所要上传的文件数量过多.\n" + (message === 0 ? "您已达到上传限制." : "您可以选择 " + (message > 1 ? "最多 " + message + " 个文件." : "1个文件."));
			    var dialog = new CloudDisk.MessageDialog(msg, '提示');
		        dialog.show();
				return;
			}

			switch (errorCode) {
				case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
					file.progress = "文件过大";
					this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
					break;
				case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
					file.progress = "Cannot upload Zero Byte files.";
					this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
					break;
				case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
					file.progress = "无效文件格式";
					this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
					break;
				default:
					if (file !== null) {
						file.progress = "未知异常";
					}
					this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
					break;
			}
			CloudDisk.UploadProgressDialog.update(file);
		} catch (ex) {
			this.debug(ex);
		}
	}
	
	function uploadCompleteHandler(file) {
	}
	
	function uploadErrorHandler(file, errorCode, message) {
	    var log = CloudDisk.Util.log;
		try {
			switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				log("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
				log("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				log("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				log("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				log("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
				log("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				log("Cancelled:" + file.name);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				log("Stopped:" + file.name);
				break;
			default:
				log("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				break;
			}
			file.progress = "上传错误";
			CloudDisk.UploadProgressDialog.update(file);
		} catch (ex) {
			this.debug(ex);
		}
    }

	return {
	    init: function(config){

			uploader = new SWFUpload({
				upload_url : CloudDisk.Setting.UPLOAD_URL,
				debug:false,
				flash_url : "js/lib/swfupload.swf",
				button_image_url: config.buttonImage,
				button_placeholder_id : config.buttonId,
				button_text: '<span class="theFont">' + config.text + '</span>',
				button_text_style: config.textStyle,
				button_text_left_padding: config.leftTextPadding,
				button_text_top_padding: config.topTextPadding,
				button_width: config.width,
				button_height: config.height,
				file_queued_handler : fileQueuedHandler,
				file_queue_error_handler : fileQueueErrorHandler,
				upload_start_handler : function(numFilesSelected, numFilesQueued){
					uploadStartHandler.apply(this, [numFilesSelected, numFilesQueued]);
				    if(config.uploadStartHandler){
					    config.uploadStartHandler();
					}
				},
				upload_error_handler : uploadErrorHandler,
				upload_progress_handler : uploadProgressHandler,
				upload_success_handler : uploadSuccessHandler,
				file_dialog_complete_handler: fileDialogComplete		
			});
		}
	};
})();